package ar.com.cema.methodology.analyzer.model;

import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="surveys")
public class Survey extends PersistentObject {
	
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(
			name="survey_questions", 
			joinColumns=@JoinColumn(name="survey_id"),
	        inverseJoinColumns=@JoinColumn(name="question_id"))
	private Set<Question> questions;
	
	@OneToMany(mappedBy="survey", cascade=CascadeType.ALL)
	private Set<Answer> answers;
	
	@Embedded
	private Person person;
	
	@Column(name="answered_date", nullable=true)
	private Date timestamp;
	
	public Survey() {
		super();
		this.questions = new TreeSet<Question>();
		this.answers = new TreeSet<Answer>();
	}
	
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public Set<Question> getQuestions() {
		return questions;
	}
	public void setQuestions(Set<Question> questions) {
		this.questions = questions;
	}
	public Set<Answer> getAnswers() {
		return answers;
	}
	public void setAnswers(Set<Answer> answers) {
		this.answers = answers;
	}

	public Date getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(Date timestamp) {
		this.timestamp = timestamp;
	}
	
	public boolean hasBeenAnswered() {
		if ((!person.getEmail().isEmpty()) && (!answers.isEmpty()) ) {
			return true;
		} else {
			return false;
		}
	}
}
